////////////////////////////////////////////////////////////////////////////
//
//  Crytek Engine Source File.
//  Copyright (C), Crytek Studios, 2001.
// -------------------------------------------------------------------------
//  File name:   paneldisplaylayer.cpp
//  Version:     v1.00
//  Created:     9/4/2002 by Timur.
//  Compilers:   Visual C++ 7.0
//  Description: 
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////


// PanelDisplayLayer.cpp : implementation file
//

#include "StdAfx.h"
#include "PanelDisplayLayer.h"

#include <ISourceControl.h>
#include "ObjectLayerPropsDialog.h"
#include "Objects\ObjectLayerManager.h"

#define ITEM_COLOR_NORMAL RGB(0,0,0)
#define ITEM_COLOR_SELECTED RGB(255,0,0)
#define ITEM_COLOR_FROZEN RGB(196,196,196)


enum
{
	ID_LAYERS_SETTINGS = 1,
	ID_LAYERS_DELETE,
	ID_LAYERS_EXPORT,
	ID_LAYERS_SEPARATOR,
	ID_LAYERS_ADD,
	ID_LAYERS_IMPORT,
	ID_LAYERS_SEPARATOR2,
	ID_LAYERS_EXPAND,
	ID_LAYERS_COLLAPSE,
};


//////////////////////////////////////////////////////////////////////////
//CLayerToolbar
//
//////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(CLayerToolbar, CDlgToolBar)
	ON_WM_RBUTTONUP()
END_MESSAGE_MAP()


//////////////////////////////////////////////////////////////////////////
void CLayerToolbar::OnRButtonUp(UINT nFlags, CPoint point)
{
	if(m_pParent)
		m_pParent->ShowContextMenu( );
}


//////////////////////////////////////////////////////////////////////////
// CPanelDisplayLayer dialog
//
//////////////////////////////////////////////////////////////////////////

class CLayerEditorViewPaneClass : public TRefCountBase<IViewPaneClass>
{
	//////////////////////////////////////////////////////////////////////////
	// IClassDesc
	//////////////////////////////////////////////////////////////////////////
	virtual ESystemClassID SystemClassID() { return ESYSTEM_CLASS_VIEWPANE; };

	virtual REFGUID ClassID()
	{
		// {A55E3527-FB72-4af4-B278-12002374DAFB}
		static const GUID guid = 
		{ 0xa55e3527, 0xfb72, 0x4af4, { 0xb2, 0x78, 0x12, 0x0, 0x23, 0x74, 0xda, 0xfb } };
		return guid;
	}
	virtual const char* ClassName() { return LAYER_EDITOR_NAME; };
	virtual const char* Category() { return "Editor"; };
	//////////////////////////////////////////////////////////////////////////
	virtual CRuntimeClass* GetRuntimeClass() { return RUNTIME_CLASS(CPanelDisplayLayer); };
	virtual const char* GetPaneTitle() { return _T(LAYER_EDITOR_NAME); };
	virtual EDockingDirection GetDockingDirection() { return DOCK_FLOAT; };
	virtual CRect GetPaneRect() { return CRect(100,100,300,1000); };
	virtual bool SinglePane() { return true; };
	virtual bool WantIdleUpdate() { return false; };
};

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::RegisterViewClass()
{
	GetIEditor()->GetClassFactory()->RegisterClass( new CLayerEditorViewPaneClass );
	GetIEditor()->GetSettingsManager()->AddToolVersion(LAYER_EDITOR_NAME,LAYER_EDITOR_VER);
}

IMPLEMENT_DYNCREATE(CPanelDisplayLayer, CToolbarDialog)
CPanelDisplayLayer::CPanelDisplayLayer(CWnd* pParent /*=NULL*/)
: CToolbarDialog(CPanelDisplayLayer::IDD, pParent)
{
	m_pLayerManager = GetIEditor()->GetObjectManager()->GetLayersManager();
	m_bLayersValid = false;
	m_bIgnoreSelectItem = false;
	m_nPrevHeight = 0;

	Create( IDD,pParent );

	// Register callback.
	m_pLayerManager->AddUpdateListener( functor(*this,&CPanelDisplayLayer::OnLayerUpdate) );
}

CPanelDisplayLayer::~CPanelDisplayLayer()
{
	m_pLayerManager->RemoveUpdateListener( functor(*this,&CPanelDisplayLayer::OnLayerUpdate) );
}

void CPanelDisplayLayer::OnDestroy()
{
	m_pLayerManager->RemoveUpdateListener( functor(*this,&CPanelDisplayLayer::OnLayerUpdate) );

	__super::OnDestroy();
}

void CPanelDisplayLayer::DoDataExchange(CDataExchange* pDX)
{
	CToolbarDialog::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_LAYERS, m_treeCtrl);
	DDX_Control(pDX,IDC_LAYERS,m_layersCtrl );
}


BEGIN_MESSAGE_MAP(CPanelDisplayLayer, CToolbarDialog)
	ON_WM_DESTROY()
	ON_WM_RBUTTONUP()
	ON_WM_VKEYTOITEM()
	ON_WM_PAINT()
	ON_LBN_SELCHANGE(IDC_LAYERS, OnSelChanged)
	//ON_NOTIFY(LBN_LAYERS_RBUTTON_DOWN, IDC_LAYERS, OnLayersRButtonDown)
	ON_NOTIFY(LBN_LAYERS_RBUTTON_UP, IDC_LAYERS, OnLayersRButtonUp)

	ON_COMMAND( ID_PANEL_LAYERS_NEW,OnBnClickedNew )
	ON_COMMAND( ID_PANEL_LAYERS_RENAME,OnBnClickedRename )
	ON_COMMAND( ID_PANEL_LAYERS_DELETE,OnBnClickedDelete )
	ON_COMMAND( ID_PANEL_LAYERS_EXPORT,OnBnClickedExport )
	ON_COMMAND( ID_PANEL_LAYERS_IMPORT,OnBnClickedImport )
	ON_COMMAND( ID_PANEL_LAYERS_ADDCHILD,OnBnClickedAddChildLayer )
	
//	ON_NOTIFY(TVN_CHKCHANGE, IDC_OBJECTS, OnTvnCheckBox)
END_MESSAGE_MAP()

BOOL CPanelDisplayLayer::OnInitDialog()
{
	CToolbarDialog::OnInitDialog();

	m_hPrevSelected = 0;

	//m_treeCtrl.EnableMultiSelect(TRUE);
	//m_treeCtrl.SetBkColor( RGB(0xE0,0xE0,0xE0) );
	m_layersCtrl.SetBkColor( GetSysColor(COLOR_BTNFACE) );
//	m_layers.SetUpdateCallback( functor(*this,OnLayersUpdate) );
	ReloadLayers();

	m_toolbar.Create( this,WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_TOOLTIPS|CBRS_FLYBY );
	m_toolbar.SetPanelDisplayLayer( this );
	m_toolbar.LoadToolBar24( IDR_PANEL_LAYERS,14 );
	RecalcBarLayout();

	// Autoresize layers.
	SetResize( IDC_LAYERS,SZ_RESIZE(1) );
	SetResize( IDC_INFOTEXT,SZ_REPOS(1) );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::OnLayerUpdate( int event,CObjectLayer *pLayer )
{
	switch (event)
	{
	case CObjectLayerManager::ON_LAYER_SELECT:
		SelectLayer( pLayer );
		break;
	case CObjectLayerManager::ON_LAYER_MODIFY:
		UpdateLayerItem( pLayer );
		break;
	case CObjectLayerManager::ON_LAYER_CHANGESIZE:
		ChangeControlSize();
		break;
	default:
		m_bLayersValid = false;
		ReloadLayers();
	}
}

//////////////////////////////////////////////////////////////////////////
HTREEITEM CPanelDisplayLayer::AddLayer( CObjectLayer *pLayer )
{
	return 0;
	m_bIgnoreSelectItem = true;
	HTREEITEM hRoot = TVI_ROOT;
	if (pLayer->GetParent())
	{
		hRoot = stl::find_in_map( m_layerToItemMap,pLayer->GetParent(),hRoot );
	}
	HTREEITEM hItem = m_treeCtrl.InsertItem( pLayer->GetName(),0,0,hRoot );
	m_layerToItemMap[pLayer] = hItem;
	m_treeCtrl.SetItemData( hItem,(DWORD_PTR)pLayer );
	m_bIgnoreSelectItem = false;
	
	SetItemState( hItem );

	// Add child layers.
	for (int i = 0; i < pLayer->GetChildCount(); i++)
	{
		AddLayer( pLayer->GetChild(i) );
	}
	return hItem;
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::UpdateLayerItem( CObjectLayer *pLayer )
{
	HTREEITEM hItem = stl::find_in_map( m_layerToItemMap,pLayer,(HTREEITEM)0 );
	if (hItem)
		SetItemState(hItem);
	m_layersCtrl.ReloadLayers();
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::SetItemState( HTREEITEM hItem )
{
	return;
	CObjectLayer *pLayer = (CObjectLayer*)m_treeCtrl.GetItemData(hItem);
	assert(pLayer);

	if (pLayer->IsVisible())
	{
		m_treeCtrl.SetCheck( hItem,1 );
	}
	else
	{
		m_treeCtrl.SetCheck( hItem,0 );
	}

	
	LOGFONT lf;
	memset(&lf, 0, sizeof(LOGFONT));       // zero out structure
//	lf.lfHeight = 12;                      // request a 12-pixel-height font
	if (pLayer->IsFrozen())
		lf.lfItalic = TRUE;
//	strcpy(lf.lfFaceName, "Tahoma");        // request a face name "Arial"

	m_treeCtrl.SetItemFont( hItem,lf );
	m_treeCtrl.RedrawWindow();
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::ReloadLayers()
{
	m_layersCtrl.SetBkColor( GetSysColor(COLOR_BTNFACE) );
	m_layersCtrl.ReloadLayers();
	return;

	m_bIgnoreSelectItem = true;
	m_treeCtrl.SetRedraw(FALSE);
	m_treeCtrl.DeleteAllItems();
	m_bIgnoreSelectItem = false;

	m_hPrevSelected = 0;
	m_layerToItemMap.clear();

	m_bLayersValid = true;
	std::vector<CObjectLayer*> layers;
	m_pLayerManager->GetLayers( layers );

	CObjectLayer *pCurrent = m_pLayerManager->GetCurrentLayer();

	CString selectedLayerName;
	
	// Add root layers.
	CLayersListBox::Layers layersInfo;
	for (int i = 0; i < layers.size(); i++)
	{
		CObjectLayer *pLayer = layers[i];
		if (pLayer->GetParent())
			continue;

		CLayersListBox::SLayerInfo layerInfo;
		layerInfo.name = pLayer->GetName();
		layerInfo.visible = pLayer->IsVisible();
		layerInfo.usable = !pLayer->IsFrozen();
		layerInfo.isModified = pLayer->IsModified();
		layersInfo.push_back(layerInfo);

		if (pCurrent == pLayer)
		{
			selectedLayerName = m_currentLayer->GetName();
		}

		HTREEITEM hItem = AddLayer( pLayer );
		m_treeCtrl.Expand( hItem,TVE_EXPAND );
	}
	
	SelectLayer( m_pLayerManager->GetCurrentLayer() );

	int sel = m_layersCtrl.FindString( -1,selectedLayerName );
	if (sel != LB_ERR)
	{
		m_layersCtrl.SetCurSel(sel);
	}
//	m_layers.SetLayers( layersInfo );
	//m_layers.SelectLayer( selectedLayerName );
	m_treeCtrl.SetRedraw(TRUE);
}

//////////////////////////////////////////////////////////////////////////
CString CPanelDisplayLayer::GetSelectedLayer()
{
	/*
	CLayersListBox::Layers layersInfo;
	m_layers.GetLayers( layersInfo );
	int sel = m_layers.GetCurSel();
	if (sel != LB_ERR)
	{
		return layersInfo[sel].name;
	}
	*/
	return "";
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::OnLayersUpdate()
{
	/*
	// Update layers info.
	CUndo undo( "Layer Modify" );
	CLayersListBox::Layers layersInfo;
	m_layers.GetLayers( layersInfo );
	for (int i = 0; i < layersInfo.size(); i++)
	{
		CObjectLayer *pLayer = m_pLayerManager->FindLayerByName(layersInfo[i].name);
		if (!pLayer)
			continue;
		pLayer->SetVisible( layersInfo[i].visible );
		pLayer->SetFrozen( !layersInfo[i].usable );
	}
	*/
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::SelectLayer( CObjectLayer *pLayer )
{
	// Current layer possibly change.	
	assert(pLayer);
	m_pLayerManager->SetCurrentLayer( pLayer );

	m_bIgnoreSelectItem = true;
	CString selectedLayerName = pLayer->GetName();
	int sel = m_layersCtrl.FindString( -1,selectedLayerName );
	if (sel != LB_ERR)
	{
		m_layersCtrl.SetCurSel(sel);
	}
	m_bIgnoreSelectItem = false;

	return;

	HTREEITEM hItem = stl::find_in_map( m_layerToItemMap,pLayer,(HTREEITEM)0 );
	assert( hItem );

	m_bIgnoreSelectItem = true;

	if (m_hPrevSelected)
	{
		m_treeCtrl.SetItemState(m_hPrevSelected,0,TVIS_SELECTED|TVIS_FOCUSED);
		m_treeCtrl.SetItemBold( m_hPrevSelected,FALSE );
		m_treeCtrl.SetItemColor( m_hPrevSelected,ITEM_COLOR_NORMAL );
	}
	m_treeCtrl.SelectItem( hItem );
	m_treeCtrl.SetItemBold( hItem,TRUE );
	m_treeCtrl.SetItemColor( hItem,ITEM_COLOR_SELECTED );
	m_hPrevSelected = hItem;

	SetItemState( hItem );

	m_bIgnoreSelectItem = false;
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::OnSelChanged()
{
	CObjectLayer *pLayer = m_layersCtrl.GetCurrentLayer();
	if (pLayer)
	{
		m_pLayerManager->SetCurrentLayer( pLayer );
	}
}

bool CPanelDisplayLayer::CreateNewLayer(CObjectLayer* parent)
{
	// Create a new layer.
	CObjectLayerPropsDialog dlg( this );
	if (dlg.DoModal() == IDOK)
	{
		CString layerName = dlg.GetName();
		if (layerName.IsEmpty())
			return false;

		// Check if layer with such name already exists.
		if (m_pLayerManager->FindLayer(parent, layerName))
		{
			Warning( "Layer %s already exist, choose different layer name",(const char*)layerName );
			return false;
		}
		
		CUndo undo( "New Layer" );
		CObjectLayer *pLayer = m_pLayerManager->CreateLayer();
		pLayer->SetName( layerName );
		pLayer->SetExternal( dlg.m_bExternal );
		pLayer->SetExportable( dlg.m_bExportToGame );
		pLayer->SetExportLayerPak( dlg.m_bExportLayerPak );
		pLayer->SetDefaultLoaded( dlg.m_bDefaultLoaded );
		pLayer->SetFrozen( dlg.m_bFrozen );
		pLayer->SetVisible( dlg.m_bVisible );
		pLayer->SetColor(dlg.m_SolidColor.GetColor());
		pLayer->SetHavePhysics(dlg.m_bHavePhysics);
		pLayer->SetModified();
		if (parent)
			parent->AddChild(pLayer);
		ReloadLayers();
	}

	return true;
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::OnBnClickedNew()
{
	CreateNewLayer(NULL);
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::OnBnClickedDelete()
{
	CObjectLayer *pLayer = m_pLayerManager->GetCurrentLayer();
	CString selectedLayer = pLayer->GetName();
	CString str;
	CBaseObjectsArray objects;
	GetIEditor()->GetObjectManager()->GetObjects( objects,pLayer );
	str.Format( "Deleting of layer will also delete all child layers and objects assigned to them.\r\n"
		"Delete Layer %s? It has %d objects and %d immediate child layers.",
		(const char*)selectedLayer, objects.size(), pLayer->GetChildCount());
	if (MessageBox( str,"Confirm Delete Layer",MB_YESNO|MB_ICONQUESTION	) == IDYES)
	{
		// Delete selected layer.
		CUndo undo( "Delete Layer" );
		m_pLayerManager->DeleteLayer( pLayer );
		ReloadLayers();
	}
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::OnBnClickedExport()
{
	// Export selected layer.
	CObjectLayer *pLayer = m_pLayerManager->GetCurrentLayer();
	if (!pLayer)
		return;

	char szFilters[] = "Object Layer (*.lyr)|*.lyr||";

	CString filename = "*.lyr";
	if (CFileUtil::SelectSaveFile( szFilters,"lyr","",filename ))
	{
		CWaitCursor wait;
		
		XmlNodeRef root = XmlHelpers::CreateXmlNode( "ObjectLayer" );

		XmlNodeRef layerDesc = root->newChild("Layer");
		CObjectArchive ar(GetIEditor()->GetObjectManager(),layerDesc,FALSE);
		GetIEditor()->GetObjectManager()->GetLayersManager()->ExportLayer( pLayer,ar,true );
		XmlHelpers::SaveXmlNode( root, filename );
	}
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::OnBnClickedImport()
{
	std::vector<CString> files;
	if( CFileUtil::SelectFiles( "Object Layer (*.lyr)|*.lyr|All Files|*.*||", GetIEditor()->GetLevelFolder() + "\\Layers\\", files ) )
	{
		CWaitCursor wait;

		// Start recording errors.
		CErrorsRecorder errorsRecorder;

		CUndo undo( "Import Layer(s)" );

		for (std::vector<CString>::iterator pFile = files.begin(); pFile != files.end(); ++pFile)
		{
			XmlNodeRef root = XmlHelpers::LoadXmlFromFile( *pFile );
			if (!root)
				continue;

			CObjectArchive archive( GetIEditor()->GetObjectManager(),root,true );
			for (int i = 0; i < root->getChildCount(); i++)
			{
				XmlNodeRef layerDesc = root->getChild(i);
				if (stricmp(layerDesc->getTag(),"Layer") != 0)
					continue;

				archive.node = layerDesc;
				GetIEditor()->GetObjectManager()->GetLayersManager()->ImportLayer( archive, false );
			}
			GetIEditor()->GetObjectManager()->GetLayersManager()->ResolveLayerLinks();
			archive.ResolveObjects();
		}
	}
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::OnBnClickedAddChildLayer()
{
	CreateNewLayer(m_pLayerManager->GetCurrentLayer());
}
	
//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::OnBnClickedRename()
{
	// Export selected layer.
	CObjectLayer *pLayer = m_pLayerManager->GetCurrentLayer();
	if (!pLayer)
		return;

	CObjectLayerPropsDialog dlg( this );
	dlg.SetName(pLayer->GetName());
	dlg.m_bExportToGame = pLayer->IsExportable();
	dlg.m_bExportLayerPak = pLayer->IsExporLayerPak();
	dlg.m_bDefaultLoaded = pLayer->IsDefaultLoaded();
	dlg.m_bExternal = pLayer->IsExternal();
	dlg.m_bFrozen = pLayer->IsFrozen();
	dlg.m_bVisible = pLayer->IsVisible();
	dlg.m_bHavePhysics = pLayer->IsPhysics();
	dlg.m_bMainLayer = false;
	dlg.m_color = pLayer->GetColor();
	if (dlg.DoModal() == IDOK)
	{
		CString layerName = dlg.GetName();
		if (layerName.IsEmpty())
			return;

		CObjectLayer* existing = m_pLayerManager->FindLayer(pLayer->GetParent(), layerName);
		if (NULL != existing && existing != pLayer)
		{
			Warning( "Layer %s already exist, choose different layer name",(const char*)layerName );
			return;
		}
		
		CUndo undo("Rename Layer");

		pLayer->SetName( dlg.m_name, true );
		pLayer->SetExternal( dlg.m_bExternal );
		pLayer->SetExportable( dlg.m_bExportToGame );
		pLayer->SetExportLayerPak( dlg.m_bExportLayerPak );
		pLayer->SetDefaultLoaded( dlg.m_bDefaultLoaded );
		pLayer->SetFrozen( dlg.m_bFrozen );
		pLayer->SetVisible( dlg.m_bVisible );
		pLayer->SetColor(dlg.m_SolidColor.GetColor());
		pLayer->SetHavePhysics( dlg.m_bHavePhysics);
		pLayer->SetModified();

		GetIEditor()->GetObjectManager()->GetLayersManager()->NotifyLayerChange( pLayer );
		GetIEditor()->Notify(eNotify_OnInvalidateControls);
		ReloadLayers();
	}
}


//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::OnLayersRButtonUp(NMHDR* pNMHDR, LRESULT* pResult)
{
	// Assume that layer was already selected.
	CFileUtil::ExtraMenuItems eMItems;
	eMItems.AddItem("Settings");
	eMItems.AddItem("Delete");
	eMItems.AddItem("Export");
	eMItems.AddItem("");

	ShowContextMenu(&eMItems);

	m_layersCtrl.Invalidate();

	*pResult = 1;
}


//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::ChangeControlSize()
{
	int nCnt = m_layersCtrl.GetCount();
	if(!nCnt)
		return;

	int nHeight = m_layersCtrl.GetItemHeight(0) * nCnt;

	if(nHeight == m_nPrevHeight)
		return;

	m_nPrevHeight = nHeight;

	CRect rc, ctrlRc, ctrlClientRc;
	GetWindowRect(rc);
	m_layersCtrl.GetWindowRect(ctrlRc);
	m_layersCtrl.GetClientRect(ctrlClientRc);

	int nDeltaTop = ctrlRc.top - rc.top;
	int nDeltaBottom = ctrlRc.bottom - rc.bottom;
	int nDeltaClientHeight = ctrlRc.Height() - ctrlClientRc.Height();

	CWnd* pParent = GetParent();
	if(pParent)
	{
		CRect parentRc, newRC;
		pParent->GetWindowRect(parentRc);

		newRC.left -= parentRc.left;
		newRC.top -= parentRc.top;
		newRC.right -= parentRc.left;
		newRC.bottom = newRC.top + nHeight + nDeltaTop + nDeltaBottom + nDeltaClientHeight;

		MoveWindow(newRC);
	}
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::ShowContextMenu(CFileUtil::ExtraMenuItems* pEMItems)
{
	CObjectLayer* pLayer = 0;

	int cmd = ID_LAYERS_SETTINGS;
	CFileUtil::ExtraMenuItems eMItems;
	if(!pEMItems)
	{
		pEMItems = &eMItems;
		cmd = ID_LAYERS_ADD;
	}
	else
	{
		if(m_pLayerManager->GetCurrentLayer() && m_pLayerManager->GetCurrentLayer()->IsExternal())
			pLayer = m_pLayerManager->GetCurrentLayer();
	}

	pEMItems->AddItem("Add New Layer");
	pEMItems->AddItem("Import Layers");

	pEMItems->AddItem("");

	pEMItems->AddItem("Expand All Layers");
	pEMItems->AddItem("Collapse All Layers");

	if(pLayer)
	{
		CString file = pLayer->GetExternalLayerPath();
		uint32 attr = GetIEditor()->GetSourceControl()->GetFileAttributes( file );
		CFileUtil::PopupMenu(Path::GetFile(file), Path::GetPath(file), this, 0, pEMItems);
		uint32 newAttr = GetIEditor()->GetSourceControl()->GetFileAttributes( file );
		if(attr!=SCC_FILE_ATTRIBUTE_INVALID && (attr & SCC_FILE_ATTRIBUTE_READONLY) && newAttr!=SCC_FILE_ATTRIBUTE_INVALID && !(newAttr & SCC_FILE_ATTRIBUTE_READONLY))
			pLayer->SetFrozen(false);
	}
	else
	{
		CMenu menu;
		menu.CreatePopupMenu();
		for(int i=0; i<pEMItems->names.size(); i++)
		{
			if (pEMItems->names[i].GetLength() > 0)
				menu.AppendMenu( MF_STRING, i + cmd, pEMItems->names[i] );
			else
				menu.AppendMenu( MF_SEPARATOR, 0, "");
		}
		CPoint pos;
		GetCursorPos( &pos );
		cmd = menu.TrackPopupMenu( TPM_RETURNCMD | TPM_LEFTALIGN | TPM_LEFTBUTTON, pos.x, pos.y, this );
	}

	if((!pLayer && cmd) || pEMItems->selectedIndexIfAny!=-1)
	{
		if(pLayer)
			cmd += pEMItems->selectedIndexIfAny;
		switch(cmd)
		{
		case ID_LAYERS_ADD:
			OnBnClickedNew();
			break;
		case ID_LAYERS_EXPAND:
			m_layersCtrl.ExpandAll(true);
			break;
		case ID_LAYERS_COLLAPSE:
			m_layersCtrl.ExpandAll(false);
			break;
		case ID_LAYERS_SETTINGS:
			OnBnClickedRename();
			break;
		case ID_LAYERS_EXPORT:
			OnBnClickedExport();
			break;
		case ID_LAYERS_IMPORT:
			OnBnClickedImport();
			break;
		case ID_LAYERS_DELETE:
			OnBnClickedDelete();
			break;
		}
	}
}


//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::OnRButtonUp(UINT nFlags, CPoint point)
{
	ShowContextMenu( );
}

//////////////////////////////////////////////////////////////////////////
int CPanelDisplayLayer::OnVKeyToItem(UINT nKey, CListBox* pListBox, UINT nIndex)
{
	switch (nKey)
	{
	case VK_F2:
		OnBnClickedRename();
		break;
	};

	return __super::OnVKeyToItem(nKey, pListBox, nIndex);
}

//////////////////////////////////////////////////////////////////////////
void CPanelDisplayLayer::OnPaint()
{
	m_layersCtrl.SetBkColor( GetSysColor(COLOR_BTNFACE) ); // Update while skining changed
	__super::OnPaint();
}
